# coding:utf-8
from typing import Union

from PyQt6.QtCore import Qt, QPoint
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QHBoxLayout, QVBoxLayout, QApplication, QFrame
from qfluentwidgets import IconWidget, BodyLabel, FluentIconBase, CaptionLabel, FluentIcon, setFont, \
    SimpleCardWidget, MessageBox, Action, VerticalSeparator
from qfluentwidgets.components.material import AcrylicMenu

from common import plugin_manager


class PluginItemCard(SimpleCardWidget):
    def __init__(self, icon: Union[FluentIconBase, QIcon, str],
                 title: str, content: str,
                 author: str = '作者',
                 version: str = 'V1.0.0',
                 parent=None):
        super().__init__(parent)
        self.horizontalLayout = QHBoxLayout(self)
        self.verticalLayout = QVBoxLayout()
        self.horizontalLayout_2 = QHBoxLayout()
        self.iconWidget = IconWidget(icon, self)
        self.titleLabel = BodyLabel(title, self)
        self.authorLabel = CaptionLabel(author, self)
        self.versionLabel = CaptionLabel(version, self)
        self.contentLabel = CaptionLabel(content, self)

        self.__initWidgets()
        self.__initLayout()

    def __initWidgets(self):
        self.setMaximumHeight(120)
        self.iconWidget.setFixedSize(80, 80)
        setFont(self.titleLabel, 20)
        self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
        self.customContextMenuRequested.connect(self.onContextMenuRequested)

    def __initLayout(self):
        self.horizontalLayout.setSpacing(20)
        self.horizontalLayout.setContentsMargins(20, 20, 20, 20)
        self.horizontalLayout.addWidget(self.iconWidget)
        self.verticalLayout.setSpacing(10)
        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignLeft)
        self.verticalLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignTop)

        self.horizontalLayout_2.setSpacing(10)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.horizontalLayout_2.addWidget(self.authorLabel)
        vs = VerticalSeparator(self)
        vs.setFixedHeight(15)
        self.horizontalLayout_2.addWidget(vs)
        self.horizontalLayout_2.addWidget(self.versionLabel)
        self.verticalLayout.addLayout(self.horizontalLayout_2)

        self.verticalLayout.addWidget(self.contentLabel,1)
        self.horizontalLayout.addLayout(self.verticalLayout)

    def onUnloaded(self):
        dialog = MessageBox(
            title="卸载插件",
            content="你确定要卸载插件吗？\n卸载后将无法恢复！",
            parent=self.window()
        )
        dialog.setClosableOnMaskClicked(True)
        dialog.widget.setMinimumWidth(500)
        if dialog.exec():
            key = self.property('key')
            if not key:
                return
            # plugin_manager.plugins[self.property('key')].unload()

    def enable(self):
        key = self.property('key')
        plugin = plugin_manager.plugins[key]
        if plugin.isEnabled():
            return
        plugin.enabled()

    def disabled(self):
        key = self.property('key')
        plugin = plugin_manager.plugins[key]
        if not plugin.isEnabled():
            return
        plugin.disabled()

    def mousePressEvent(self, e):
        if e.button() == Qt.MouseButton.LeftButton:
            self.clicked.emit()
        QFrame.mousePressEvent(self, e)

    def onContextMenuRequested(self, pos: QPoint):
        menu = AcrylicMenu(parent=self)
        enableAction = Action(FluentIcon.PLAY, "启用插件", self)
        enableAction.triggered.connect(self.enable)
        disableAction = Action(FluentIcon.PAUSE, "禁用插件", self)
        disableAction.triggered.connect(self.disabled)
        deleteAction = Action(FluentIcon.DELETE, "卸载插件", self)
        deleteAction.triggered.connect(self.onUnloaded)
        descriptionAction = Action(FluentIcon.INFO, "插件详情", self)
        descriptionAction.triggered.connect(lambda: print("插件详情"))
        menu.addAction(enableAction)
        menu.addAction(disableAction)
        menu.addAction(deleteAction)
        menu.exec(self.mapToGlobal(pos))


if __name__ == '__main__':
    app = QApplication([])
    card = PluginItemCard(FluentIcon.PIE_SINGLE, "插件名称", "插件描述")
    card.show()
    app.exec()
